package com.umfb.bootstrap.intercepter;

import com.google.gson.Gson;
import com.umfb.bootstrap.commons.ResponseResult;
import org.apache.log4j.Logger;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * Created by gzheng on 17-11-16.
 */
public class AbstractInterceptor extends HandlerInterceptorAdapter {

    protected Logger log = Logger.getLogger(this.getClass());

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)  throws Exception {
    }

    protected void response(HttpServletResponse response, ResponseResult rslt) throws IOException {
        response.setContentType("text/json;charset=UTF-8");
        response.getWriter().print(new Gson().toJson(rslt));
        System.out.println(new Gson().toJson(rslt));
        response.getWriter().close();
    }
}